package vcp.client.GUI.mvc.model;

import java.io.Serializable;
import java.util.List;

import vcp.client.interfaces.IObservable;
import vcp.client.request.SiteMapSaveRequest;
import vcp.entity.Site;
import vcp.entity.Worker;
import vcp.enums.SiteEnum;

/**
 * 
 * @author VCP team
 *         <p>
 *         site map model
 *         </p>
 * 
 */
public class UISiteMapModel extends UIAbstractModel
{
	/** get robot instance **/
	private Worker worker;

	public Worker getWorker ()
	{
		return this.worker;
	}

	public void setWorker ( Worker worker )
	{
		this.worker = worker;
	}

	/**
	 * 
	 * @return List of sites
	 */
	public List <Site> getSites ()
	{
		return this.worker.getSites();
	}

	/**
	 * 
	 * @param model
	 */
	public UISiteMapModel ( IObservable model )
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * save site request
	 * 
	 * @param site
	 */
	public void saveSiteRequest ( Site site )
	{
		SiteMapSaveRequest request = new SiteMapSaveRequest(site);
		this.sendRequest(request, true);
	}

	/**
	 * get site on demande
	 * 
	 * @param newSite
	 * @return site
	 */
	public Site getSiteOnDemand ( SiteEnum newSite )
	{
		Site requestedSite = null;
		for ( Site site : this.worker.getSites() )
		{
			if ( site.getSite() == newSite )
			{
				requestedSite = site;
			}
		}
		return requestedSite;
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * update worker site by site instance
	 * 
	 * @param site
	 */
	private void updateWorkerSites ( Site site )
	{
		for ( int i = 0; i < this.worker.getSites().size(); i ++ )
		{
			if ( this.worker.getSites().get(i).getSiteID() == site.getSiteID() )
			{
				this.worker.getSites().remove(i);
				this.worker.getSites().add(i, site);
				break;
			}
		}
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/
	@Override
	public void shouldRegisterForUpdates ()
	{
		this.model.registerForUpdates(this);
	}

	@Override
	public void clearModel ()
	{
		this.worker = null;
	}

	/****************************************************************************************************************************
	 *** Observer methods ***
	 ****************************************************************************************************************************/
	@SuppressWarnings ( "unchecked" )
	@Override
	public void update ( Object data )
	{
		if ( data instanceof List <?> )
		{
			List <Serializable> serializableInfo = (List <Serializable>) data;
			this.updateWorkerSites((Site) serializableInfo.get(0));
			if ( serializableInfo.size() > 1 )
			{
				String notification = (String) serializableInfo.get(1);
				this.controllerDelegate.updateControllerDelegateWithData(notification);
			}

			this.controllerDelegate.updateControllerDelegateWithData(serializableInfo.get(0));
			this.controllerDelegate.loadingStop();
		}
	}

	@Override
	public void updateWithReferesh ( Object data )
	{
		if ( data instanceof Site )
		{
			Site site = (Site) data;
			this.updateWorkerSites(site);

			this.controllerDelegate.updateControllerDelegateWithData(data);
		}
	}

}
